package com.whacos.common.vo;

import com.whacos.common.utils.DateUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xiongdun
 * @description ResponseVO
 * @since 2018/11/17 17:11
 */
public class ResponseVO extends HashMap<String, Object> {
    private static final long serialVersionUID = -2457459485523058050L;

    public static final int ERROR_CODE = 1;
    public static final int SUCCESS_CODE = 0;

    public ResponseVO() {
        put("code", SUCCESS_CODE);
        put("msg", "操作成功");
        put("datetime", DateUtils.getDatetime());
    }

    /**
     * 请求失败
     * @return
     */
    public static ResponseVO error() {
        return error(ERROR_CODE, "操作失败");
    }

    public static ResponseVO error(String msg) {
        return error(500, msg);
    }

    public static ResponseVO error(int code, String msg) {
        ResponseVO response = new ResponseVO();
        response.put("code", code);
        response.put("msg", msg);
        return response;
    }

    public static ResponseVO success(String msg) {
        ResponseVO response = new ResponseVO();
        response.put("msg", msg);
        return response;
    }

    /**
     * 请求成功
     * @param map
     * @return
     */
    public static ResponseVO success(Map<String, Object> map) {
        ResponseVO response = new ResponseVO();
        response.putAll(map);
        return response;
    }

    public static ResponseVO success(Object obj) {
        ResponseVO response = new ResponseVO();
        response.put("data", obj);
        return response;
    }

    public static ResponseVO success() {
        return new ResponseVO();
    }

    @Override
    public ResponseVO put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
